package com.elmcode.server.task;

import com.elmcode.server.elmenum.OrderStatusEnum;
import com.elmcode.server.entity.BookEntity;
import com.elmcode.server.service.BookService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class OrderCancelTask {

    @Resource
    BookService bookService;

    @Scheduled(cron="0 0/30 * * * ? ")
    public void bookTask(){

        System.out.println("订单长时间未支付，即将取消订单");

        List<BookEntity> entities = bookService.orderCancel();
        if(entities != null){
            List<Object> collect = entities.stream().map(item -> {
                item.setStatus(OrderStatusEnum.CANCELLED.getCode());
                item.setCancelTime(new Date());
                item.setCancelReason("订单长时间未支付，系统自动取消");

                bookService.updateByNum(item);
                return null;
            }).collect(Collectors.toList());
        }

    }

}

